<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>大事件-后台登录</title>
    <link rel="stylesheet" href="./css/reset.css">
    <!-- 1.0 导入bootstrap样式 -->
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./libs/layui/css/modules/layer/default/layer.css">
    <link rel="stylesheet" href="./css/login.css">
</head>

<body>
    <div class="container">
        <!-- logo -->
        <img class="logo" src="./images/logo.png" alt="">

        <!-- 登录框 -->
        <div class="panel panel-success">
            <div class="panel-heading">
                <h3 class="panel-title"></h3>
            </div>
            <div class="panel-body">
                <!-- 登录框表单 -->
                <form id="login_form" class="form-horizontal">
                    <div class="form-group">
                        <div class="col-sm-12 item">
                            <input type="text" class="form-control" id="username" name="username" required placeholder="请输入用户名">
                            <!-- 用户图标 -->
                            <i class="glyphicon glyphicon-user"></i>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12 item">
                            <input type="password" class="form-control" id="password" name="password" required placeholder="请输入密码">
                            <!-- 密码图标 -->
                            <i class="glyphicon glyphicon-lock"></i>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12">
                            <button type="button" id="login" class="btn btn-success btn-block">登录</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/layui/lay/modules/layer.js"></script>
    <script>
        $(function() {
            //给form表单submit事件
            $('.btn').click(function() {
                //获取用户输入数据,拿到的是键值对 username= &password=
                let formData = $('.form-horizontal').serialize();
                //发送ajax请求
                // console.log(
                //     formData
                // );
                let index = null;
                $.ajax({
                    url: 'http://localhost:8080/api/v1/admin/user/login',
                    type: 'POST',
                    data: formData,
                    //在发出ajax之前执行 加载层
                    beforeSend: function() {
                        //登录时的动画
                        index = layer.load()
                            //index代表的是加载时的动画值,类似定时器的做法
                    },
                    //数据响应回来后执行
                    complete: function() {
                        //关闭正在登录的提示
                        layer.close(index)
                    },
                    success: function(resData) {
                        //判断如果状态不是200,提示错误
                        if (resData.code != 200) {
                            alert(resData.msg);
                            return
                        }
                        //将响应回来的数据存储到本地存储
                        sessionStorage.setItem('bigNewsToken', resData.token);
                        //跳转页面
                        window.location = './index.html'
                    }
                })
            })
        })
    </script>
</body>

</html>